package com.hc.model.utils;

import com.hc.DTO.TokenDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class TokenModel {
	public static TokenModel instance;

	public static TokenModel getInstance() {
		if (instance == null)
			instance = new TokenModel();
		return instance;
	}

	public TokenDTO getToken(Context context) {
		TokenDTO dto = new TokenDTO();
		String sql = "SELECT * FROM token";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			dto.setAccess_token(c.getString(c.getColumnIndex("access_token")));
			dto.setToken_type(c.getString(c.getColumnIndex("token_type")));
			dto.setExpires(c.getString(c.getColumnIndex("expires")));
		}
		c.close();
		db.close();

		return dto;
	}

	public boolean updateToken(TokenDTO dto, Context context) {
		boolean flag = false;
		// String sql = "UPDATE changelog SET Id=" + id;
		DBHelper db = new DBHelper(context);
		ContentValues values = new ContentValues();
		values.put("access_token", dto.getAccess_token());
		values.put("token_type", dto.getToken_type());
		values.put("expires", dto.getExpires());
		flag = db.ExcuteUpdate(null, "token", values, "1 = 1", null);
		db.close();
		return flag;
	}
}
